/**
 * 
 */
package com.vector.ding.web.service.common;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.vector.ding.commons.util.StringUtil;
import com.vector.ding.commons.util.collection.CollectionUtil;
import com.vector.ding.dao.mybatis.mapper.MtpjpfInfoMapper;
import com.vector.ding.dao.mybatis.model.MtpjpfInfo;
import com.vector.ding.dao.mybatis.model.MtpjpfInfoExample;

/**
 * @author vector
 *
 */
@Service
public class MediaScoreService {
	
	@Autowired
	private MtpjpfInfoMapper mediaScoreMapper;
	
	public MtpjpfInfo getOne(String companyName) {
		List<MtpjpfInfo> list = getList(companyName);
		return CollectionUtil.isEmpty(list) ? null : list.get(0);
	}
	
	public MtpjpfInfo getOne(String companyName, int belongYear) {
		if (StringUtil.isEmpty(companyName))
			return null;
		
		MtpjpfInfoExample example = new MtpjpfInfoExample();
		example.or().andCompanyNameEqualTo(companyName)
			.andBelongYearEqualTo(belongYear);
		List<MtpjpfInfo> list = mediaScoreMapper.selectByExample(example);
		return CollectionUtil.isEmpty(list) ? null : list.get(0);
	}

	public List<MtpjpfInfo> getList(String companyName) {
		if (StringUtil.isEmpty(companyName))
			return Collections.emptyList();
		
		MtpjpfInfoExample example = new MtpjpfInfoExample();
		example.or().andCompanyNameEqualTo(companyName);
		List<MtpjpfInfo> list = mediaScoreMapper.selectByExample(example);
		if (CollectionUtil.isEmpty(list))
			return Collections.emptyList();
		return list;
	}
	
	public List<MtpjpfInfo> getList(String companyName, int pageIndex, int pageSize) {
		if (StringUtil.isEmpty(companyName))
			return Collections.emptyList();
		
		PageHelper.startPage(pageIndex, pageSize, true);
		MtpjpfInfoExample example = new MtpjpfInfoExample();
		example.or().andCompanyNameEqualTo(companyName);
		List<MtpjpfInfo> list = mediaScoreMapper.selectByExample(example);
		if (CollectionUtil.isEmpty(list))
			return Collections.emptyList();
		return list;
	}
	
	public BigDecimal getScore(String companyName, int belongYear) {
		MtpjpfInfo info = getOne(companyName, belongYear);
		return info == null ? null : info.getNewsGrade();
	}
	
	public BigDecimal getScore(String companyName) {
		List<MtpjpfInfo> list = getList(companyName);
		if (CollectionUtil.isEmpty(list))
			return new BigDecimal(0);
		
		BigDecimal score = new BigDecimal(0);
		for (MtpjpfInfo info: list) {
			score = score.add(info.getNewsGrade());
		}
		return score;
	}
}
